<?php
namespace App\Repositories\GameServer;

use App\Models\GameServer\RechargeRecord;
use Carbon\Carbon;
use App\Repositories\GameServer\LoginRecordRepository;
use App\Repositories\GamerRepository;
use DB;

class RechargeRecordRepository
{
    //获取某玩家付费记录
    public function getRechargeRecords($id)
    {
        $res = RechargeRecord::where('account_id', $id)->with('goods')->orderBy('id','desc')->get();

        return $res;
    }

    //累计付费用户数量
    public static function getRechargeGamerNum()
    {
        $count = RechargeRecord::select('account_id')->distinct()->get()->count();

        return $count;
    }

    /**
     * [付费率 = 30日内付费人数 / 登录总人数]
     * @return [type] [description]
     */
    public static function getRechargeRate111()
    {
        $month = Carbon::now()->subMonth();

        // 30日内付费人数统计
        $recharge_count = self::getMonthRechargeGamerNum($month->toDateTimeString());
        //登录总人数
        $login_count = LoginRecordRepository::getMonthActiveNum();
        $recharge_rate = $login_count > 0 ? round($recharge_count / $login_count, 4) : 0;

        return $recharge_rate * 100 . '%';
    }

    /**
     * [付费率 = 付费人数 / 总人数] ---最新公式
     * @return [type] [description]
     */
    public static function getRechargeRate()
    {
        $recharge_count = self::getRechargeGamerNum();
        $amount = GamerRepository::getGamerNum();

        $recharge_rate = $amount > 0 ? round($recharge_count / $amount, 4) : 0;

        return $recharge_rate * 100 . '%';
    }

    //根据日期筛选用户数量
    public static function getMonthRechargeGamerNum($date)
    {
        return RechargeRecord::select('account_id')
                                ->distinct()
                                ->where('buy_time', '>=', $date)
                                ->get()
                                ->count();
    }


    //总的ARPU = 当月活跃用户付费总额 / 当月付费用户数量  --弃用
    public static function getArpu111()
    {
        $month = Carbon::now()->startOfMonth();

        //当月活跃用户id
        $ids = LoginRecordRepository::getMonthActiveGamerIds();
        //当月活跃用户付费总额
        $recharge_total = RechargeRecord::whereIn('account_id', $ids)->sum('price');
        //当月付费用户数量
        $recharge_gamer_amount = self::getMonthRechargeGamerNum($month->toDateTimeString());

        $arpu = $recharge_gamer_amount > 0 ? round($recharge_total / $recharge_gamer_amount, 2) : 0;

        return $arpu;

    }

    //总的ARPU = 累计付费金额 / 新增总人数   最新公式 新增总人数 == 总人数
    public static function getArpu()
    {
        $sum = self::getSumRechargeAmount();
        $amount = GamerRepository::getGamerNum();

        $arpu = $amount > 0 ? round($sum / $amount, 2) : 0;
        return $arpu;

    }


    //累计付费金额
    public static function getSumRechargeAmount()
    {
        $amount = RechargeRecord::sum('price');

        return round($amount, 2);
    }

    //30日LTV = 30天的付费金额 / 30天总人数
    public static function getLTV()
    {
        $month = Carbon::now()->subMonth();

        //30天内的付费金额
        $recharge_gamer_amount = self::getSumOfRechargeByDate($month);
        $gamer_amount = LoginRecordRepository::getMonthActiveNum();

        $ltv = $gamer_amount > 0 ? round($recharge_gamer_amount / $gamer_amount, 2) : 0;

        return $ltv;
    }

    //根据日期统计付费金额
    public static function getSumOfRechargeByDate($date)
    {
        $sum = RechargeRecord::where('buy_time', '>=', $date->toDateTimeString())->sum('price');

        return $sum;
    }

    //ARPPU = 累计付费金额 / 付费用户数量
    public static function getArppu()
    {
        $amount = self::getSumRechargeAmount();
        $gamer_amount = self::getRechargeGamerNum();

        $arppu = $gamer_amount > 0 ? round($amount / $gamer_amount, 2) : 0;

        return $arppu;
    }

    public function addRechargeRecord($arr)
    {
        return RechargeRecord::create($arr);
    }

    //付费玩家 -- 渠道时间分组
    public function getPayGamerOfChannel($where)
    {
        $start = date('Y-m-d', $where['start']);
        $end = date('Y-m-d', $where['end']);
        $channel = $where['channel'];

        $gamers = RechargeRecord::select('distributer_id',DB::raw('date_format(buy_time,"%Y-%m-%d")date, count(distinct(account_id))count'))
                                ->when($channel, function ($query) use ($channel) {
                                    return $query->whereIn('distributer_id', $channel);
                                })
                                ->whereBetween('buy_time', [$start, $end])
                                ->groupBy('distributer_id', 'date')
                                ->get();

        return $gamers;
    }

    //付费金额 -- 渠道时间分组
    public function getRechargeAmountOfChannel($where)
    {
        $start = date('Y-m-d', $where['start']);
        $end = date('Y-m-d', $where['end']);
        $channel = $where['channel'];

        $gamers = RechargeRecord::select('distributer_id',DB::raw('date_format(buy_time,"%Y-%m-%d") as date, sum(price) as count'))
                                ->when($channel, function ($query) use ($channel) {
                                    return $query->whereIn('distributer_id', $channel);
                                })
                                ->whereBetween('buy_time', [$start, $end])
                                ->groupBy('distributer_id', 'date')
                                ->get();
        return $gamers;
    }
}
